package com.viewpager2.teach.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.viewpager2.teach.TextFragment;

import java.util.List;

/**
 * Creator:Yx
 * Time:2020/8/5 9:35
 * Description:TabLayout + ViewPager2 +Fragment-  Adapter 继承FragmentStateAdapter
 */
public class ViewPager2ThreeAdapter extends FragmentStateAdapter {
    private List<String> mLists;

    public ViewPager2ThreeAdapter(FragmentActivity fm) {
        super(fm);
    }

    public void setFragmentCount(List<String> list) {
        this.mLists = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mLists != null ? mLists.size() : 0;
    }


    @NonNull
    @Override
    public Fragment createFragment(int position) {
        //这里可以通过类型控制，返回不同界面的Fragment
        //这里有问题：第一个Fragment可见时只创建了一个，点击第二个TAB时，突然创建了二和三号Fragment
        return TextFragment.getInstance("页面" + position);
    }

}
